/*
 * This file is part of JBotSim.
 * 
 *    JBotSim is free software: you can redistribute it and/or modify it
 *    under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *  
 *    Authors:
 *    Arnaud Casteigts		<casteig@site.uottawa.ca>
 */
package jbotsim.ui;
import javax.swing.JOptionPane;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Point2D;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import jbotsim.Clock;
import jbotsim.Node;
import jbotsim.Topology;
import jbotsim.Topology;

import _examples.PRouteNode;
import _examples.aodvnode;
import _examples.global;
import _examples.team;
/**
 * The viewer includes a central jtopology which will draw the attached 
 * topology and offer interaction, as well as contextual commands to add or
 * remove a communication range or sensing range tuners (slider bars), or to 
 * pause/resume the system clock.
 */
public class JViewer{
	public JTopology jtp;
	protected JSlider cRTuner, sRTuner;
	protected JFrame window=null;
	protected EventHandler handler=new EventHandler();
	public int teamtype=1;
	public int cx;
        public  int cy;
        public Vector<team> teamv=new Vector<team>();
        int leftoffset=40;
        int topoffset=40;
        int offset=40;
        Topology topo1;
	  public Vector<PRouteNode> node=new Vector<PRouteNode>();
	 public Vector<aodvnode> nodebr=new Vector<aodvnode>();
	/**
	 * Creates a windowed viewer for the specified topology. 
	 * @param topo The topology to be drawn and/or manipulated.
	 */
    public JViewer(Topology topo){
    	this(topo, true);
    }
	/**
	 * Creates a viewer for the specified topology. If <tt>selfContained</tt>
	 * is <tt>true</tt>, a new window will be created to contain the viewer
	 * (similarly to <tt>JViewer(Topology)</tt>). If it is <tt>false</tt>, 
	 * no window will be created and the viewer can be subsequently
	 * integrated to another swing container (e.g. another <tt>JFrame</tt> 
	 * or a <tt>JApplet</tt>).
	 * @param topo The topology to be drawn and/or manipulated.
	 */
     
    public JViewer(Topology topo, boolean selfContained){
         
     
    }
    
    
	/**
	 * Creates a windowed viewer encapsulating the specified jtopology. 
	 * @param jtopo The jtopology to be encapsulated.
	 */
    public JViewer(JTopology jtopo){
    	this(jtopo, true);
    }
    public void settopo1(Topology topo)
    {
         topo1=topo;
    }
	/**
	 * Creates a viewer encapsulating the specified jtopology. If 
	 * <tt>selfContained</tt> is <tt>true</tt>, a new window will be created 
	 * to contain the viewer (similarly to <tt>JViewer(Topology)</tt>). If it 
	 * is <tt>false</tt>, no window will be created and the viewer can be 
	 * subsequently integrated to another swing container (e.g. another 
	 * <tt>JFrame</tt> or a <tt>JApplet</tt>).
	 * @param jtopo The JTopology to be encapsulated.
	 */
    public JViewer(JTopology jtopo, boolean selfContained){
    	this.jtp=jtopo;
    
        jtp.addActionCommand("添加飞行编队");
   		jtp.addActionCommand("communication bar");
   		//jtp.addActionCommand("sensing bar");
   		jtp.addActionCommand("clock on/off");
   		//jtp.addActionCommand("toString()");
   		jtp.addActionListener(handler);
    	if (selfContained){
	   		window=new JFrame();
	   		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	   		window.add(jtp);
	   		Dimension dim = (Dimension)jtp.topo.getProperty("dimension");
	   		Dimension pos = (Dimension)jtp.topo.getProperty("position");
	   		if (dim==null) dim = new Dimension(800,600);
	   		if (pos==null) pos = new Dimension(0,0);
	   		window.setBounds(pos.width, pos.height, dim.width, dim.height);
	   		window.setVisible(true);
    	}
    }
    /**
     * Returns the jtopology attached to this viewer. Obtaining the reference 
     * can be useful for example to add or remove action commands or action 
     * listeners to the jtopology.
     * @return The jtopology reference.
     */
    public JTopology getJTopology(){
    	return jtp;
    }
    /**
     * Sets the size of the inner jtopology to the specified dimension.
     * @param width The desired width, in pixels.
     * @param height The desired height, in pixels.
     */
    public void setSize(int width, int height){
		jtp.setPreferredSize(new Dimension(width,height));
    	if (window!=null)
    		window.pack();
    }
    /**
     * Adds a communication range tuner (slider bar) to regulate the
     * communication range of the topology's nodes. The tuner is anchored at
     * the north of the jtopology.
     */
    public void addCommunicationRangeTuner(){
    	cRTuner=new JSlider(0,800,(int)Node.getModel("default").getCommunicationRange());
		cRTuner.addChangeListener(handler);
		jtp.getParent().add(cRTuner,BorderLayout.NORTH);
	}
    /**
     * Removes the communication range tuner (slider bar), if any.
     */
    public void removeCommunicationRangeTuner(){
 		jtp.getParent().remove(cRTuner);
 		cRTuner=null;
    }
    /**
     * Adds a sensing range tuner (slider bar) to regulate the sensing range of
     * the topology's nodes. The tuner is anchored at the south of the 
     * jtopology.
     */
    public void addSensingRangeTuner(){
    	sRTuner=new JSlider(0,800,(int)Node.getModel("default").getSensingRange());
		sRTuner.addChangeListener(handler);
		jtp.getParent().add(sRTuner,BorderLayout.SOUTH);
	}
    /**
     * Removes the sensing range tuner (slider bar), if any.
     */
    public void removeSensingRangeTuner(){
 		jtp.getParent().remove(sRTuner);
 		sRTuner=null;
    }
    public void setteamtye(int i)
    {
        teamtype=i;
    }
    
    public void setteammovetype(int index,int type)
    {
        int i=0;
         team  t=teamv.get(index);
         t.movetype=type;
    
        for(i=0;i<t.teamv.size();i++)
        {
           t.teamv.get(i).type=type;
           t.teamv2.get(i).type=type;
        }
    }
    
    
    public void moveteams1()
    {
        int i=0;
        
        for(i=0;i<teamv.size();i++)
        {
            teamv.get(i).teammove(global.o.getnodes().get(0).getX(), global.o.getnodes().get(0).getY(), 4);
           
        }
    }
    
    
    public void tocross(int num,int teamid,int index)
    {
        if(teamid==-1)
        {
            //new 
            int len=(num-1)/4;
            int lenb=num-1-3*len;
            int i;
             team te=new team(index);
            teamv.add(te);
            //水平
                 Node t=Node.newInstanceOfModel("default");
                      Node t2=Node.newInstanceOfModel("aodv"); 
               
                      global.o2.addNode(cx ,cy,t2);
                 topo1.addNode(cx ,cy,t);
                 te.addnode(t);
                 te.addnode2(t2);
                Point2D.Double target=new Point2D.Double(cx,cy);te.targets.add(target);
            for(i=1;i<=len;i++)
            {
                 t=Node.newInstanceOfModel("default");
                      t2=Node.newInstanceOfModel("aodv"); 
                      int offsett=offset*i;
                      global.o2.addNode(cx+offsett,cy,t2);
                 topo1.addNode(cx+offsett,cy,t);
                 te.addnode(t);
                 te.addnode2(t2);
                  te.targets.add(target);
                 
                 
                    t=Node.newInstanceOfModel("default");
                      t2=Node.newInstanceOfModel("aodv");
                      global.o2.addNode(cx-offsett,cy,t2);
                 topo1.addNode(cx-offsett,cy,t);
                 te.addnode(t);
                 te.addnode2(t2);
                  te.targets.add(target);
                   t=Node.newInstanceOfModel("default");
                       t2=Node.newInstanceOfModel("aodv");
                      global.o2.addNode(cx,cy-offsett,t2);
                 topo1.addNode(cx,cy-offsett,t);
                 te.addnode(t);
                 te.addnode2(t2);
                 te.targets.add(target);
            }
              
                 for(i=1;i<=lenb;i++)
            {
                 t=Node.newInstanceOfModel("default");
                       t2=Node.newInstanceOfModel("aodv");
                      global.o2.addNode(cx,cy+offset*i,t2);
                 topo1.addNode(cx,cy+offset*i,t);
                 te.addnode(t);
                 te.addnode2(t2);
                 te.targets.add(target);
            }
   
        }
        else
        {
            //change
              //new 
            
           
            team te=teamv.get(teamid);
            int num1=te.targets.size();
             int len=(num1-1)/4;
            int lenb=num1-1-3*len;
            int i;
           te.movetype=99;
           double x0=te.teamv.get(0).getX();
            double y0=te.teamv.get(0).getY();
           
            //水平
             int off=1;
             
                  
                 
            for(i=1;i<=len;i++)
            {
                    double offsett=offset*i;
                 te.teamv.get(off).setDirection(new Point2D.Double(x0+offsett, y0+offsett));
                  te.teamv2.get(off).setDirection(new Point2D.Double(x0+offsett, y0+offsett));
                   te.targets.set(off,new Point2D.Double(x0+offsett, y0+offsett));
                  off++;
                  te.teamv.get(off).setDirection(new Point2D.Double(x0-offsett, y0));
                  te.teamv2.get(off).setDirection(new Point2D.Double(x0-offsett, y0));
                 te.targets.set(off,new Point2D.Double(x0-offsett, y0));
                 off++;
                  te.teamv.get(off).setDirection(new Point2D.Double(x0, y0-offsett));
                  te.teamv2.get(off).setDirection(new Point2D.Double(x0, y0-offsett));
                   te.targets.set(off,new Point2D.Double(x0, y0-offsett));
                 off++;
             
                 
            }
              
                 for(i=1;i<=lenb;i++)
            {
                    te.teamv.get(off).setDirection(new Point2D.Double(x0, y0+offset*i));
                  te.teamv2.get(off).setDirection(new Point2D.Double(x0, y0+offset*i));
                    te.targets.set(off,new Point2D.Double(x0, y0+offset*i));
                 off++;
              
                 
            }
        }
    }
    
    
  
    
    
      public void toV(int num,int teamid,int index)
    {
        if(teamid==-1)
        {
            //new 
            int len=(num-1)/2;
            int lenb=num-1-len;
            int i;
             team te=new team(index);
            teamv.add(te);
            //水平
                 Node t=Node.newInstanceOfModel("default");
                      Node t2=Node.newInstanceOfModel("aodv"); 
               
                      global.o2.addNode(cx ,cy,t2);
                 topo1.addNode(cx ,cy,t);
                 te.addnode(t);
                 te.addnode2(t2);
                  Point2D.Double target=new Point2D.Double(cx,cy);
                  te.targets.add(target);
            for(i=1;i<=len;i++)
            {
                 t=Node.newInstanceOfModel("default");
                      t2=Node.newInstanceOfModel("aodv"); 
                      int offsett=offset*i;
                      global.o2.addNode(cx+offsett,cy+offsett,t2);
                 topo1.addNode(cx+offsett,cy+offsett,t);
                 te.addnode(t);
                 te.addnode2(t2);
        te.targets.add(target);
            }
              
                 for(i=1;i<=lenb;i++)
            {
                int offsett=offset*i;
                 t=Node.newInstanceOfModel("default");
                       t2=Node.newInstanceOfModel("aodv");
                      global.o2.addNode(cx-offsett,cy+offsett,t2);
                 topo1.addNode(cx-offsett,cy+offsett,t);
                 te.addnode(t);
                 te.addnode2(t2);
                  te.targets.add(target);
            }
   
        }
        else
        {
              team te=teamv.get(teamid);
            int num1=te.targets.size();
                 int len=(num1-1)/2;
            int lenb=num1-1-len;
                int i;
      
           te.movetype=99;
           double x0=te.teamv.get(0).getX();
            double y0=te.teamv.get(0).getY();
            
            //水平
             int off=1;
                    for(i=1;i<=len;i++)
            {
                   int offsett=offset*i;
                  te.teamv.get(off).setDirection(new Point2D.Double(x0+offsett, y0+offsett));
                  te.teamv2.get(off).setDirection(new Point2D.Double(x0+offsett, y0+offsett));
                 te.targets.set(off,new Point2D.Double(x0+offsett, y0+offsett));
                 off++;
           
      
            }
              
                 for(i=1;i<=lenb;i++)
            {
                int offsett=offset*i;
                  te.teamv.get(off).setDirection(new Point2D.Double(x0-offsett, y0+offsett));
                  te.teamv2.get(off).setDirection(new Point2D.Double(x0-offsett, y0+offsett));
                 te.targets.set(off,new Point2D.Double(x0-offsett, y0+offsett));
                 off++;
                
                 
            }
             
            //change
            
        }
    }
    
    
    
    
    
      
        
      public void toSquare(int num,int teamid,int index)
    {
        if(teamid==-1)
        {
            //new 
            int len1=(num-4)/2;
            int lenh=len1/2;
            int lenl=(num-4-2*lenh)/2;
            int remain=num-4-2*lenl-2*lenh;

            int i;
             team te=new team(index);
            teamv.add(te);
            //水平
              
                  if(remain==1)
                  {
                      Node   t=Node.newInstanceOfModel("default");
                   Node   t2=Node.newInstanceOfModel("aodv"); 
       
                      global.o2.addNode(cx-offset,cy,t2);
                 topo1.addNode(cx-offset,cy,t);
                 te.addnode(t);
                 te.addnode2(t2);
                  Point2D.Double target=new Point2D.Double(cx,cy);te.targets.add(target);
                  }
                 
            for(i=0;i<=(lenl+1);i++)
            {
               Node  t=Node.newInstanceOfModel("default");
                   Node   t2=Node.newInstanceOfModel("aodv"); 
                      int offsett=offset*i;
                      global.o2.addNode(cx+offsett,cy,t2);
                 topo1.addNode(cx+offsett,cy,t);
                 te.addnode(t);
                 te.addnode2(t2);
                  Point2D.Double target=new Point2D.Double(cx,cy);te.targets.add(target);
                  t=Node.newInstanceOfModel("default");
                      t2=Node.newInstanceOfModel("aodv"); 
               
                      global.o2.addNode(cx+offsett,cy+offset*(lenh+1),t2);
                 topo1.addNode(cx+offsett,cy+offset*(lenh+1),t);
                 te.addnode(t);
                 te.addnode2(t2);   
                  te.targets.add(target);
            }
              
                 for(i=1;i<=lenh;i++)
            {
                int offsett=offset*i;
            Node     t=Node.newInstanceOfModel("default");
                Node       t2=Node.newInstanceOfModel("aodv");
                      global.o2.addNode(cx,cy+offsett,t2);
                 topo1.addNode(cx,cy+offsett,t);
                 te.addnode(t);
                 te.addnode2(t2);
                  Point2D.Double target=new Point2D.Double(cx,cy);te.targets.add(target);
                 
        
                 t=Node.newInstanceOfModel("default");
                       t2=Node.newInstanceOfModel("aodv");
                      global.o2.addNode(cx+offset*(lenl+1),cy+offsett,t2);
                 topo1.addNode(cx+offset*(lenl+1),cy+offsett,t);
                 te.addnode(t);
                 te.addnode2(t2);
                 te.targets.add(target);
            }
   
        }
        else
        {
            //change
               //new 
              team te=teamv.get(teamid);
            int num1=te.targets.size();
            int len1=(num1-4)/2;
            int lenh=len1/2;
            int lenl=(num1-4-2*lenh)/2;
            int remain=num1-4-2*lenl-2*lenh;

        
              
            
                     int i;
   
           te.movetype=99;
           double x0=te.teamv.get(0).getX();
            double y0=te.teamv.get(0).getY();
            
            //水平
             int off=1;
           
                 
                 
                  if(remain==1)
                  {           
                  te.teamv.get(off).setDirection(new Point2D.Double(x0-offset, y0));
                  te.teamv2.get(off).setDirection(new Point2D.Double(x0-offset, y0));
                  Point2D.Double target=new Point2D.Double(x0-offset, y0);
                  te.targets.set(off,target);
                 off++;
        
                  }
                 
            for(i=0;i<=(lenl+1);i++)
            {
                
                
                  int offsett=offset*i;
                  te.teamv.get(off).setDirection(new Point2D.Double(x0+offsett, y0));
                  te.teamv2.get(off).setDirection(new Point2D.Double(x0+offsett, y0));
                  Point2D.Double target=new Point2D.Double(x0+offsett, y0);
                 te.targets.set(off,target);
                 off++;
                 
     
                  te.teamv.get(off).setDirection(new Point2D.Double(x0+offsett, y0+offset*(lenh+1)));
                  te.teamv2.get(off).setDirection(new Point2D.Double(x0+offsett, y0+offset*(lenh+1)));
                 te.targets.set(off,new Point2D.Double(x0+offsett, y0+offset*(lenh+1)));
                 off++;
             
            }
              
                 for(i=1;i<=lenh;i++)
            {
                int offsett=offset*i;
         te.teamv.get(off).setDirection(new Point2D.Double(x0, y0+offsett));
                  te.teamv2.get(off).setDirection(new Point2D.Double(x0, y0+offsett));
                    te.targets.set(off,new Point2D.Double(x0, y0+offsett));
                 off++;
                 
                   te.teamv.get(off).setDirection(new Point2D.Double(x0+offset*(lenl+1), y0+offsett));
                  te.teamv2.get(off).setDirection(new Point2D.Double(x0+offset*(lenl+1), y0+offsett));
                 te.targets.set(off,new Point2D.Double(x0+offset*(lenl+1), y0+offsett));
                 off++;
                 
        
             
                 
            }
        }
    }
      
      
        public void toArrow(int num,int teamid,int index)
    {
        if(teamid==-1)
        {
            //new 
            
            
            
            int lenl=(num-1)/3;
         
            int lenh=num-1-2*lenl;
         

            int i;
             team te=new team(index);
            teamv.add(te);
            //水平
                 Node t=Node.newInstanceOfModel("default");
                      Node t2=Node.newInstanceOfModel("aodv"); 
               
                 
                     
       
                      global.o2.addNode(cx,cy,t2);
                 topo1.addNode(cx,cy,t);
                 te.addnode(t);
                 te.addnode2(t2);
          Point2D.Double target=new Point2D.Double(cx,cy);te.targets.add(target);
                 
            for(i=1;i<=lenl;i++)
            {
                 t=Node.newInstanceOfModel("default");
                      t2=Node.newInstanceOfModel("aodv"); 
                      int offsett=offset*i;
                      global.o2.addNode(cx-offsett,cy+offsett,t2);
                 topo1.addNode(cx-offsett,cy+offsett,t);
                 te.addnode(t);
                 te.addnode2(t2);
                 
                  te.targets.add(target);
                 
                 
                     t=Node.newInstanceOfModel("default");
                      t2=Node.newInstanceOfModel("aodv"); 
                
                      global.o2.addNode(cx+offsett,cy+offsett,t2);
                 topo1.addNode(cx+offsett,cy+offsett,t);
                 te.addnode(t);
                 te.addnode2(t2);
            te.targets.add(target);
            }
              
                 for(i=1;i<=lenh;i++)
            {
                int offsett=offset*i;
                 t=Node.newInstanceOfModel("default");
                       t2=Node.newInstanceOfModel("aodv");
                      global.o2.addNode(cx,cy+offsett,t2);
                 topo1.addNode(cx,cy+offsett,t);
                 te.addnode(t);
                 te.addnode2(t2);
         te.targets.add(target);
            }
   
        }
        else
        {
            //change
              team te=teamv.get(teamid);
            int num1=te.targets.size();
                 int lenl=(num1-1)/3;
         
            int lenh=num1-1-2*lenl;
         

         
                     int i;
   
           te.movetype=99;
           double x0=te.teamv.get(0).getX();
            double y0=te.teamv.get(0).getY();
            
            //水平
             int off=1;
         
                 
            for(i=1;i<=lenl;i++)
            {
                   int offsett=offset*i;
         te.teamv.get(off).setDirection(new Point2D.Double(x0-offsett, y0+offsett));
                  te.teamv2.get(off).setDirection(new Point2D.Double(x0-offsett, y0+offsett));
                  te.targets.set(off,new Point2D.Double(x0-offsett, y0+offsett));
                 off++;
              
                   te.teamv.get(off).setDirection(new Point2D.Double(x0+offsett, y0+offsett));
                  te.teamv2.get(off).setDirection(new Point2D.Double(x0+offsett, y0+offsett));
                   te.targets.set(off,new Point2D.Double(x0+offsett, y0+offsett));
                 off++;
                 
             
           
            }
              
                 for(i=1;i<=lenh;i++)
            {
                
                   int offsett=offset*i;
         te.teamv.get(off).setDirection(new Point2D.Double(x0, y0+offsett));
                  te.teamv2.get(off).setDirection(new Point2D.Double(x0, y0+offsett));
                 te.targets.set(off,new Point2D.Double(x0, y0+offsett));
                 off++;
             
        
            }
        }
    }
      
      
    
            public void toTri(int num,int teamid,int index)
    {
        if(teamid==-1)
        {
            //new 
     
            int lenl=(num-3)/3;
         
            
         

            int i;
             team te=new team(index);
            teamv.add(te);
            //水平
                 Node t=Node.newInstanceOfModel("default");
                      Node t2=Node.newInstanceOfModel("aodv"); 
               
                 
                   
       
                      global.o2.addNode(cx,cy,t2);
                 topo1.addNode(cx,cy,t);
                 te.addnode(t);
                 te.addnode2(t2);
          Point2D.Double target=new Point2D.Double(cx,cy);te.targets.add(target);
                   double lastx=0,lasty=0;
                 
            for(i=1;i<=lenl+1;i++)
            {
                 t=Node.newInstanceOfModel("default");
                      t2=Node.newInstanceOfModel("aodv"); 
                     double offsett=offset*i;
                      global.o2.addNode(cx-offsett,cy+offsett,t2);
                 topo1.addNode(cx-offsett,cy+offsett,t);
                 te.addnode(t);
                 te.addnode2(t2);
                  te.targets.add(target);
                 
                  
                       lastx=cx-offsett;
                 lasty=cy+offsett;
                 
                     t=Node.newInstanceOfModel("default");
                      t2=Node.newInstanceOfModel("aodv"); 
                
                      global.o2.addNode(cx+offsett,cy+offsett,t2);
                 topo1.addNode(cx+offsett,cy+offsett,t);
                 te.addnode(t);
                 te.addnode2(t2);
            te.targets.add(target);
            }
              
               for(i=1;i<=(num-lenl*2-3);i++)
            {
               double offsett=offset*i*1.741;
                 t=Node.newInstanceOfModel("default");
                       t2=Node.newInstanceOfModel("aodv");
                      global.o2.addNode(lastx+offsett, lasty,t2);
                 topo1.addNode(lastx+offsett, lasty,t);
                 te.addnode(t);
                 te.addnode2(t2);
                  te.targets.add(target);
                 
            
                 
        
            }
   
        }
        else
        {
            //change
                 
              team te=teamv.get(teamid);
            int num1=te.targets.size();
            int lenl=(num1-3)/3;
         
            
         

               int i;
        
           te.movetype=99;
           double x0=te.teamv.get(0).getX();
            double y0=te.teamv.get(0).getY();
            
            //水平
             int off=1;
         double lastx=0,lasty=0;
                 
            for(i=1;i<=lenl+1;i++)
            {
                  int offsett=offset*i;
         te.teamv.get(off).setDirection(new Point2D.Double(x0-offsett, y0+offsett));
                  te.teamv2.get(off).setDirection(new Point2D.Double(x0-offsett, y0+offsett));
                 te.targets.set(off,new Point2D.Double(x0-offsett, y0+offsett));
                 off++;
              
                 
                 
                       lastx=x0-offsett;
                 lasty=y0+offsett;
                 
                
         te.teamv.get(off).setDirection(new Point2D.Double(x0+offsett, y0+offsett));
                  te.teamv2.get(off).setDirection(new Point2D.Double(x0+offsett, y0+offsett));
                 te.targets.set(off,new Point2D.Double(x0+offsett, y0+offsett));
                 off++;
            
           
            }
              
                 for(i=1;i<=(num1-lenl*2-3);i++)
            {
                double offsett=offset*i*2;
         te.teamv.get(off).setDirection(new Point2D.Double(lastx+offsett, lasty));
                  te.teamv2.get(off).setDirection(new Point2D.Double(lastx+offsett, lasty));
                   te.targets.set(off,new Point2D.Double(lastx+offsett,lasty));
                 off++;  
            }
        }
    }
            
            
         
            
        public void toStar(int num,int teamid,int index)
    {
        if(teamid==-1)
        {
            //new 
            
            
            
            int lenl=(num-1)/3;
         
            
         

            int i;
             team te=new team(index);
            teamv.add(te);
            //水平
                 Node t=Node.newInstanceOfModel("default");
                      Node t2=Node.newInstanceOfModel("aodv"); 
               
                 
                   
       
                      global.o2.addNode(cx,cy,t2);
                 topo1.addNode(cx,cy,t);
                 te.addnode(t);
                 te.addnode2(t2);
          Point2D.Double target=new Point2D.Double(cx,cy);te.targets.add(target);
                 
            for(i=1;i<=lenl;i++)
            {
                 t=Node.newInstanceOfModel("default");
                      t2=Node.newInstanceOfModel("aodv"); 
                     double offsett=offset*i;
                      global.o2.addNode(cx,cy-offsett,t2);
                 topo1.addNode(cx,cy-offsett,t);
                 te.addnode(t);
                 te.addnode2(t2);
                 te.targets.add(target);
              t=Node.newInstanceOfModel("default");
                      t2=Node.newInstanceOfModel("aodv"); 
                  
                      global.o2.addNode(cx-offsett,cy+offsett,t2);
                 topo1.addNode(cx-offsett,cy+offsett,t);
                 te.addnode(t);
                 te.addnode2(t2);
                 
                 te.targets.add(target);
                   t=Node.newInstanceOfModel("default");
                      t2=Node.newInstanceOfModel("aodv"); 
                  
                      global.o2.addNode(cx+offsett,cy+offsett,t2);
                 topo1.addNode(cx+offsett,cy+offsett,t);
                 te.addnode(t);
                 te.addnode2(t2);
            te.targets.add(target);
            }
            
             for(i=1;i<=num-1-lenl*3;i++)
            {
               t=Node.newInstanceOfModel("default");
                      t2=Node.newInstanceOfModel("aodv"); 
                   double offsett=offset*(lenl+i);
                      global.o2.addNode(cx,cy-offsett,t2);
                 topo1.addNode(cx,cy-offsett,t);
                 te.addnode(t);
                 te.addnode2(t2);
            te.targets.add(target);
            }
              
               
   
        }
        else
        {
            //change
                  team te=teamv.get(teamid);
            int num1=te.targets.size();
            int lenl=(num1-1)/3;
         
            
         

               int i;
      
           te.movetype=99;
           double x0=te.teamv.get(0).getX();
            double y0=te.teamv.get(0).getY();
            
            //水平
             int off=1;
         
                 
            for(i=1;i<=lenl;i++)
            {
                int offsett=offset*i;
         te.teamv.get(off).setDirection(new Point2D.Double(x0, y0-offsett));
                  te.teamv2.get(off).setDirection(new Point2D.Double(x0, y0-offsett));
                 te.targets.set(off,new Point2D.Double(x0, y0-offsett));
                 off++;
                
                te.teamv.get(off).setDirection(new Point2D.Double(x0-offsett, y0+offsett));
                  te.teamv2.get(off).setDirection(new Point2D.Double(x0-offsett, y0+offsett));
                   te.targets.set(off,new Point2D.Double(x0-offsett, y0+offsett));
                 off++; 
              
                    te.teamv.get(off).setDirection(new Point2D.Double(x0+offsett, y0+offsett));
                  te.teamv2.get(off).setDirection(new Point2D.Double(x0+offsett, y0+offsett));
                te.targets.set(off,new Point2D.Double(x0+offsett, y0+offsett));
                 off++; 
              
         
           
            }
                for(i=1;i<=num1-1-lenl*3;i++)
            {
                     double offsett=offset*(lenl+i);
               te.teamv.get(off).setDirection(new Point2D.Double(x0, y0-offsett));
                  te.teamv2.get(off).setDirection(new Point2D.Double(x0, y0-offsett));
                te.targets.set(off,new Point2D.Double(x0, y0-offsett));
                 off++; 
            }
               
   
        }
    }
                  
                  
                  
    
    public void createteam(int num,int index)
    {
         // node.get(desmac-1).setProperty("color", "yello");
	 //node.get(0).begsend(node.get(desmac-1).getprid(),datalen,node.get(desmac-1));
     //   JOptionPane.showInputDialog(teamtype);
        if(teamtype==1)
        {
            team te=new team(index);
            teamv.add(te);
            //水平
            int i;
            for(i=0;i<num;i++)
            {
                Node t=Node.newInstanceOfModel("default");
                      Node t2=Node.newInstanceOfModel("aodv");
                      global.o2.addNode(cx+offset*i,cy,t2);
                 topo1.addNode(cx+offset*i,cy,t);
                 te.addnode(t);
                 te.addnode2(t2);
                 te.targets.add(new Point2D.Double(cx+offset*i,cy));
            }
        }
       else    if(teamtype==2)
        {
              team te=new team(index);
            teamv.add(te);
            int i;
            int rot=1;
            for(i=0;i<num;i++)
            {
              Node t=Node.newInstanceOfModel("default");
                   Node t2=Node.newInstanceOfModel("aodv");
                      global.o2.addNode(cx,cy,t2);
                 topo1.addNode(cx,cy,t);
                 cx=cx+leftoffset*rot;
                 cy=cy+topoffset;
                    if(rot==1)
                {
                    rot=-1;
                }
                    else
                    {
                        rot=1;
                    }
                    te.addnode(t);
                    te.addnode2(t2);
                    te.targets.add(new Point2D.Double(cx,cy));
            }
        }
           
        else     if(teamtype==3)
        {
           
              team te=new team(index);
              
            teamv.add(te);
            int i=0,j;
            int rot=1;
            int level=1;
            int left=cx;
            while(true)
            {
             
                 for(j=0;j<level;j++)
                 {
                      Node t=Node.newInstanceOfModel("default");
                          Node t2=Node.newInstanceOfModel("aodv");
                      global.o2.addNode(j*leftoffset+left,cy,t2);
               
                 topo1.addNode(j*leftoffset+left,cy,t);
                 i++;
                 te.addnode(t);
                 te.addnode2(t2);
                  te.targets.add(new Point2D.Double(j*leftoffset+left,cy));
                     if(i>=num)
                         return;
                 }
                 left-=(leftoffset/2);
                 level++;            
                 cy=cy+topoffset;
                  
            }
        }
             else if(teamtype==4)
             {
                // 十字编队,
                 tocross(num,-1,index);
                 
             }
           else if(teamtype==5)
             {
                // V型编队, 
                   toV(num,-1,index);
             }
           else if(teamtype==6)
             {
               //  方形编队,
                 toSquare(num,-1,index);
             }
           else if(teamtype==7)
             {
                  //箭头编队, 
                   toArrow(num,-1,index);
             }
           else if(teamtype==8)
             {
                // 三角编队,
                 toTri(num,-1,index);
             }
          else if(teamtype==9)
             {
                // 星型编队
                  toStar(num,-1,index);
             }
              
     
    }
    public void addnode(PRouteNode nodein)
    {
    	node.add(nodein);
    }
    
       public void addnodebr(aodvnode nodein)
    {
    	nodebr.add(nodein);
    }
	class EventHandler implements ChangeListener, ActionListener{		
		public void stateChanged(ChangeEvent arg0) {
			JSlider src=((JSlider)arg0.getSource());
			if (src==cRTuner){
				for (Node n : jtp.topo.getNodes())
					n.setCommunicationRange(src.getValue());
				Node.getModel("default").setCommunicationRange(src.getValue());			
			}else{
				for (Node n : jtp.topo.getNodes())
					n.setSensingRange(src.getValue());
				Node.getModel("default").setSensingRange(src.getValue());			
			}
			jtp.updateUI();
		}
		public void actionPerformed(ActionEvent arg0) {
			String cmd=((JMenuItem)arg0.getSource()).getText();
			if (cmd.equals("communication bar")){
				if (cRTuner==null) 
					addCommunicationRangeTuner();
				else 
					removeCommunicationRangeTuner();
				jtp.updateUI();
			}else if (cmd.equals("sensing bar")){
				if (sRTuner==null)
					addSensingRangeTuner();
				else
					removeSensingRangeTuner();
				jtp.updateUI();
			}else if (cmd.equals("clock on/off")){
				if (Clock.isRunning())
					Clock.pause();
				else
					Clock.resume();
			}else if (cmd.equals("toString()")){
				System.out.println(jtp.topo);
			}else if (cmd.equals("ping����"))
			{
				//int times=0;
				int des=0;
				String  str1 = JOptionPane.showInputDialog("������Ŀ�Ľڵ�ip��ַ������macֵ��");
				
				//String  str2 = JOptionPane.showInputDialog("�������������ping����Ŀ��");
				try
				{
					des= Integer.parseInt(str1);
			        //  times = Integer.parseInt(str2);
					node.get(0).rootsendping(des);
				}
				catch( NumberFormatException e)
			       {
			           JOptionPane.showMessageDialog(
			                  null,"�ٺ٣�������Ƿ�ֵ�����ǣ�",
			                  "��ʾ����",
			                  JOptionPane.DEFAULT_OPTION );
			       }
				
			}
			else if (cmd.equals("�ڵ��ƶ�"))
			{
				//int times=0;
				int mac=0;
				String  str1 = JOptionPane.showInputDialog("������ڵ�macֵ��");
				
				//String  str2 = JOptionPane.showInputDialog("�������������ping����Ŀ��");
				try
				{
					mac= Integer.parseInt(str1);
					double x=node.get(mac-1).getcoords().x;
					double y=node.get(mac-1).getcoords().y;
					
					 JOptionPane.showMessageDialog(
			                  null,"x:"+x+"   y:"+y,
			                  "��ʾ����",
			                  JOptionPane.DEFAULT_OPTION );
					   str1 = JOptionPane.showInputDialog("������ڵ�Ŀ�����x��");
					   double xd=Double.parseDouble(str1);
					   str1 = JOptionPane.showInputDialog("������ڵ�Ŀ�����y��");
					   double yd=Double.parseDouble(str1);
					   str1 = JOptionPane.showInputDialog("������ڵ�ÿ�������˶�������");
					   int td=Integer.parseInt(str1);
					   
					   
					   node.get(mac-1).setmove(new Point2D.Double(xd, yd), td);
					
			        //  times = Integer.parseInt(str2);
					
					 
				}
				catch( NumberFormatException e)
			       {
			           JOptionPane.showMessageDialog(
			                  null,"�ٺ٣�������Ƿ�ֵ�����ǣ�",
			                  "��ʾ����",
			                  JOptionPane.DEFAULT_OPTION );
			       }
			}
                       
                                else if (cmd.equals("添加飞行编队"))
				{
					 if (Clock.isRunning())
							Clock.pause();
						else
							Clock.resume();
					//int times=0;
					int desmac=0;
					int num=0;
					String  str1 = JOptionPane.showInputDialog("请输入节点个数");
					
					//String  str2 = JOptionPane.showInputDialog("�������������ping����Ŀ��");
					try
					{
						num= Integer.parseInt(str1);
						 
						  createteam(num,-1);
                                                global.jv.jtp.addlist();
                                          
						 // node.get(desmac-1).setProperty("color", "yello");
						//  node.get(0).begsend(node.get(desmac-1).getprid(),datalen,node.get(desmac-1));
						  if (Clock.isRunning())
								Clock.pause();
							else
								Clock.resume();
						
				        //  times = Integer.parseInt(str2);
						
						 
					}
				 
				
				catch( NumberFormatException e)
			       {
			           JOptionPane.showMessageDialog(
			                  null,"�ٺ٣�������Ƿ�ֵ�����ǣ�",
			                  "��ʾ����",
			                  JOptionPane.DEFAULT_OPTION );
			       }
				
				}
				else if (cmd.equals("发送数据"))
				{
					 if (Clock.isRunning())
							Clock.pause();
						else
							Clock.resume();
					//int times=0;
					int desmac=0;
					int datalen=0;
					String  str1 = JOptionPane.showInputDialog("������ڵ�macֵ��");
					
					//String  str2 = JOptionPane.showInputDialog("�������������ping����Ŀ��");
					
				
				}
				cx=0;
                                cy=0;
			}
		}
	}

